package com.mercury.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import com.mercury.user.entity.CenterUser;
import com.mercury.user.entity.VO.UserInfoVo;
import com.mercury.user.service.CenterUserService;
import com.mercury.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 * 进行用户的注册和登录
 *
 * @author tong
 * @since 2021-10-25
 */
@RestController
@RequestMapping(value = "/user", produces = "application/json")
@Api(description = "用户模块接口")
@CrossOrigin
public class CenterUserController {

    @Autowired
    private CenterUserService userService;


    @ApiOperation(value = "root用户登录")
    @PostMapping("/root")
    //TODO 这是虚假登录，还需要需要更改
    public R loginRoot(@RequestBody CenterUser user) {
        return R.ok().data("token", "admin");
    }

    @ApiOperation(value = "返回 root 用户的 info 信息")
    @GetMapping("/info")
    //这是虚假返回信息，还需要进行更改
    public R info() {
        return R.ok().data("roles", "[admin]").data("name", "admin").data("avater", "https://img1.baidu.com/it/u=3755297117,609162545&fm=26&fmt=auto");
    }


    /**
     * @return R
     * @date 2021/10/28 8:34
     * @params body
     * @description 用户注册控制器，统一返回 R 类
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public R registerUser(@RequestBody CenterUser user) {
        boolean save = userService.addUser(user);
        if (save) {
            return R.ok();
        } else {
            return R.error().data("error", "邮箱已注册");
        }
    }

    /**
     * @return R
     * @date 2021/10/28 8:35
     * @params body
     * @description  返回个人中心所需要的所有信息，雷达图，喝过和想喝
     */
    @ApiOperation(value = "用户使用邮箱+密码的方式进行登录")
    @PostMapping("/login")
    public R loginUser(@RequestBody CenterUser user) {

        UserInfoVo userInfo = userService.getUserInfo(user);
        return R.ok().data("user", userInfo);
    }

    @ApiOperation(value = "更新用户信息")
    @PostMapping("/user")
    public R updateUser(@RequestBody CenterUser user) {

        boolean res = userService.updateById(user);
        if(res) return R.ok().message("修改成功");
        else return R.error().message("修改失败");
    }

    /**
     * @return
     * @date 2021/11/1 15:39
     * @params current
     * @params limit
     * @description 分页展示用户
     */
    @ApiOperation(value = "分页展示")
    @PostMapping("pageUser/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current,
                             @PathVariable long limit) {

        //创建page对象
        Page<CenterUser> pageUser = new Page<>(current, limit);
        userService.page(pageUser, null);
        long total = pageUser.getTotal();
        List<CenterUser> list = pageUser.getRecords();
        return R.ok().data("items", list).data("total", total);
    }

    /**
     * @return
     * @date 2021/11/3 17:49
     * @params null
     * @description 逻辑删除
     */
    @ApiOperation("逻辑删除用户")
    @DeleteMapping("/delete/{id}")
    public R deleteUser(@PathVariable String id) {

        boolean res = userService.removeById(id);
        if (res) {
            return R.ok();
        } else {
            return R.error();
        }

    }




}

